// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.network.packets.hit;

import java.nio.ByteBuffer;
import zombie.characters.IsoGameCharacter;
import zombie.core.network.ByteBufferWriter;
import zombie.core.raknet.UdpConnection;
import zombie.iso.IsoGridSquare;
import zombie.iso.IsoWorld;
import zombie.network.GameClient;
import zombie.network.GameServer;
import zombie.network.ServerMap;
import zombie.network.packets.INetworkPacket;

public class Square implements IPositional, INetworkPacket {
    protected float positionX;
    protected float positionY;
    protected float positionZ;
    protected IsoGridSquare square;

    public void set(IsoGameCharacter character) {
        this.square = character.getAttackTargetSquare();
        if (this.square != null) {
            this.positionX = (float)this.square.getX();
            this.positionY = (float)this.square.getY();
            this.positionZ = (float)this.square.getZ();
        } else {
            this.positionX = 0.0F;
            this.positionY = 0.0F;
            this.positionZ = 0.0F;
        }
    }

    public void set(IsoGridSquare _square) {
        this.square = _square;
        if (this.square != null) {
            this.positionX = (float)this.square.getX();
            this.positionY = (float)this.square.getY();
            this.positionZ = (float)this.square.getZ();
        } else {
            this.positionX = 0.0F;
            this.positionY = 0.0F;
            this.positionZ = 0.0F;
        }
    }

    @Override
    public void parse(ByteBuffer b, UdpConnection connection) {
        this.positionX = b.getFloat();
        this.positionY = b.getFloat();
        this.positionZ = b.getFloat();
        if (GameServer.bServer) {
            this.square = ServerMap.instance
                .getGridSquare((int)Math.floor((double)this.positionX), (int)Math.floor((double)this.positionY), (int)Math.floor((double)this.positionZ));
        }

        if (GameClient.bClient) {
            this.square = IsoWorld.instance
                .CurrentCell
                .getGridSquare((int)Math.floor((double)this.positionX), (int)Math.floor((double)this.positionY), (int)Math.floor((double)this.positionZ));
        }
    }

    @Override
    public void write(ByteBufferWriter b) {
        b.putFloat(this.positionX);
        b.putFloat(this.positionY);
        b.putFloat(this.positionZ);
    }

    @Override
    public String getDescription() {
        return "\n\tSquare [ pos=( " + this.positionX + " ; " + this.positionY + " ; " + this.positionZ + " ) ]";
    }

    void process(IsoGameCharacter isoGameCharacter) {
        isoGameCharacter.setAttackTargetSquare(isoGameCharacter.getCell().getGridSquare((double)this.positionX, (double)this.positionY, (double)this.positionZ));
    }

    @Override
    public float getX() {
        return this.positionX;
    }

    @Override
    public float getY() {
        return this.positionY;
    }

    public float getZ() {
        return this.positionZ;
    }

    public IsoGridSquare getSquare() {
        return this.square;
    }

    @Override
    public boolean isConsistent() {
        return this.square != null;
    }
}
